package polsl.biai.projekt.server.calculation;


/**
 * Population of Tours
 * contains Tours between all added Cities
 * @author jacek
 *
 */
public class Population {
	/**
	 * array containing tours
	 */
    private Tour[] tours;

    /**
     * constructor
     * makes a new Population
     * @param pSize - amount of tours
     * @param init - if true, tours will be are initialized,
     * if false they'll be empty
     */
    public Population(int pSize, boolean init) {
        tours = new Tour[pSize];

        if (init) {
            for (int i = 0; i < pSize; i++) {
                Tour newTour = new Tour();
                newTour.generateRandomTour();
                addTour(i, newTour);
            }
        }
    }
    

    /**
     * adds tour to the Population on given index
     * @param index 
     * @param tour
     */
    public void addTour(int index, Tour tour) {
        tours[index] = tour;
    }
    

    /**
     * returns tour from the Population from given index
     * @param index
     * @return
     */
    public Tour getTour(int index) {
        return tours[index];
    }

    /**
     * gets the shortest tour in the population
     * @return
     */
    public Tour getShortest() {
        Tour shortest = tours[0];
        
        for (int i = 1; i < populationSize(); i++) {
            if (shortest.getDistance() >= getTour(i).getDistance()) {
                shortest = getTour(i);
            }
        }
        return shortest;
    }

    
    public int populationSize() {
        return tours.length;
    }
}
